package com.mohan.controller;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mohan.common.LoginToken;
import com.mohan.pojo.User;
import com.mohan.rep.LoginRep;
import com.mohan.req.WxLoginReq;
import com.mohan.req.WxUserInfoReq;
import com.mohan.service.LoginService;
import com.mohan.service.UserService;
import com.mohan.tool.Base64CC;
import com.mohan.util.ResponseUtil;

@Controller
@RequestMapping(value = "/V1/user")
public class UserController {
	@Resource
	private LoginService loginService;
	
	@Resource
	private UserService userService;
	
	/**
	 * 微信用户登录(上传微信code)
	 * @param request
	 * @param wxReq
	 * @return
	 */
	@RequestMapping(value = "/login")
	@ResponseBody
	public Object login(HttpServletRequest request,@RequestBody WxLoginReq wxReq){
		String repStr = null;
		int errCode = 0;
		LoginRep lRep = null;
		/**
		 * 判断上传的参数
		 */
		if (wxReq.getCode() == null || "".equals(wxReq.getCode())) {
			repStr = "参数上传错误!";
			errCode = 2001; 
		} else {
			try {
				/**
				 * base64解码
				 */
				wxReq.setCode(Base64CC.decode(wxReq.getCode()));
				lRep = loginService.getOpenId(wxReq.getCode());
				if (lRep == null) {
					//登录失败
					repStr = "微信登录失败!";
					errCode = 2040; 
				}
				
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			}
		}
		
		/**
		 * 判定返回
		 */
		if (repStr == null) {
			ResponseUtil response = new ResponseUtil();
			response.defaultSucess(lRep);
			return response;
		} else {
			System.out.println("repStr : " + repStr);
			ResponseUtil response = new ResponseUtil();
			/**
			 * 对repStr进行base64编码
			 */
			try {
				response.defaultFailed(errCode, Base64CC.encode(repStr));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		}
	}
	
	@RequestMapping(value = "/userinfo")
	@ResponseBody
	public Object getUserInfo(HttpServletRequest request,@RequestBody WxUserInfoReq wxUserInfoReq){
		String repStr = null;
		int errCode = 0;
		/**
		 * 判断上传的参数
		 */
		if (wxUserInfoReq.getUserId() == null || "".equals(wxUserInfoReq.getUserId()) || wxUserInfoReq.getToken() == null || "".equals(wxUserInfoReq.getToken())) {
			repStr = "参数上传错误!";
			errCode = 2001; 
		} else {
			try {
				/**
				 * base64解码
				 */
				wxUserInfoReq.setUserId(Base64CC.decode(wxUserInfoReq.getUserId()));
				wxUserInfoReq.setToken(Base64CC.decode(wxUserInfoReq.getToken()));
				/**
				 * 登录验证
				 */
				int loginConfirm = LoginToken.loginConfirm(wxUserInfoReq.getUserId(), wxUserInfoReq.getToken());
				if (loginConfirm == 0) {
					User user = userService.getUserByUserId(wxUserInfoReq.getUserId());
					if (user != null) {
						if (!(wxUserInfoReq.getAvatarUrl() == null || "".equals(wxUserInfoReq.getAvatarUrl()))) {
							user.setAvatarUrl(Base64CC.decode(wxUserInfoReq.getAvatarUrl()));
						}
						if (!(wxUserInfoReq.getCity() == null || "".equals(wxUserInfoReq.getCity()))) {
							user.setAvatarUrl(Base64CC.decode(wxUserInfoReq.getCity()));
						}
						if (!(wxUserInfoReq.getCountry() == null || "".equals(wxUserInfoReq.getCountry()))) {
							user.setAvatarUrl(Base64CC.decode(wxUserInfoReq.getCountry()));
						}
						if (!(wxUserInfoReq.getGender() == null || "".equals(wxUserInfoReq.getGender()))) {
							user.setAvatarUrl(Base64CC.decode(wxUserInfoReq.getGender()));
						}
						if (!(wxUserInfoReq.getLanguage() == null || "".equals(wxUserInfoReq.getLanguage()))) {
							user.setAvatarUrl(Base64CC.decode(wxUserInfoReq.getLanguage()));
						}
						if (!(wxUserInfoReq.getNickName() == null || "".equals(wxUserInfoReq.getNickName()))) {
							user.setAvatarUrl(Base64CC.decode(wxUserInfoReq.getNickName()));
						}
						if (!(wxUserInfoReq.getProvince() == null || "".equals(wxUserInfoReq.getProvince()))) {
							user.setAvatarUrl(Base64CC.decode(wxUserInfoReq.getProvince()));
						}
						
						userService.updateWxUserByUserId(user);
					} else {
						repStr = "系统错误!";
						errCode = 2005;
					}
				} else if (loginConfirm == -1) {
					repStr = "请先登录!";
					errCode = 2016;
				} else if (loginConfirm == -2) {
					repStr = "账号异常!";
					errCode = 2017;
				} else {
					repStr = "验证码过期,请重新登录!";
					errCode = 2018;
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				repStr = "Base64解码失败!";
				errCode = 2019;
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				repStr = "数据库操作失败!";
				errCode = 2021;
			}
		}
		
		/**
		 * 判定返回
		 */
		if (repStr == null) {
			ResponseUtil response = new ResponseUtil();
			try {
				response.defaultSucess(Base64CC.encode("信息更改成功!"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		} else {
			System.out.println("repStr : " + repStr);
			ResponseUtil response = new ResponseUtil();
			/**
			 * 对repStr进行base64编码
			 */
			try {
				response.defaultFailed(errCode, Base64CC.encode(repStr));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return response;
		}
	}
}
